﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using WBGModel;
using System.Data;

namespace WBGDal
{
    public class DalExame
    {
        public List<ModelExame> Listar()
        {
            return Listar("");
        }

        public List<ModelExame> Listar(string filtro)
        {
            String cmd = "SELECT * FROM Exame" + (filtro.CompareTo("") == 0 ? "" : " WHERE " + filtro);
            return ConvertToList(UtilBanco.ExecComRetorno(cmd));
        }

        private List<ModelExame> ConvertToList(DataTable resultSet)
        {
            List<ModelExame> lst = new List<ModelExame>();
            for (int i = 0; i < resultSet.Rows.Count; i++)
            {
                DataRow dr = resultSet.Rows[i];
                ModelExame exame = new ModelExame();
                exame.idExame = Convert.ToInt32(dr["idExame"]);
                exame.nome = dr["nome"].ToString();
                exame.sexoAplicavel = dr["sexoAplicavel"].ToString();
                exame.exigeAcompanhante = Convert.ToBoolean(dr["exigeAcompanhante"].ToString());
                exame.ativo = Convert.ToBoolean(dr["ativo"].ToString());
                exame.observacao = dr["observacao"].ToString();
                lst.Add(exame);
            }
            return lst;
        }

        public void Inserir(ModelExame obj)
        {
            String cmd = String.Format("INSERT INTO Exame (nome,sexoAplicavel,exigeAcompanhante,ativo,observacao) VALUES ('{0}','{1}','{2}','{3}','{4}')", obj.nome.Replace("'", "''"), obj.sexoAplicavel, obj.exigeAcompanhante, obj.ativo, obj.observacao);
            UtilBanco.ExecSemRetorno(cmd);
        }

        public void Alterar(ModelExame obj)
        {
            String cmd = String.Format("UPDATE Exame SET nome='{0}', sexoAplicavel='{1}', exigeAcompanhante='{2}', ativo='{3}', observacao='{4}' WHERE idExame={5}", obj.nome.Replace("'", "''"), obj.sexoAplicavel, obj.exigeAcompanhante, obj.ativo, obj.observacao, obj.idExame);
            UtilBanco.ExecSemRetorno(cmd);
        }

        public void Deletar(int id)
        {
            String cmd = String.Format("DELETE FROM Exame WHERE idExame={0}", id);
            UtilBanco.ExecSemRetorno(cmd);
        }
    }
}
